# generate by ollama qwen:8b

import pygame
import random

# 初始化 Pygame
pygame.init()

# 屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("登录月球")

# 颜色定义
WHITE = (255, 255, 255)
GRAY = (169, 169, 169)
BLACK = (0, 0, 0)

# 游戏时钟
clock = pygame.time.Clock()
FPS = 60

# 角色参数
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 50
player_x = 100
player_y = SCREEN_HEIGHT - PLAYER_HEIGHT - 10
player_vel_y = 0
is_jumping = False
jump_strength = -15
gravity = 0.8

# 障碍物参数
OBSTACLE_WIDTH = 50
OBSTACLE_HEIGHT = 50
obstacle_x = SCREEN_WIDTH
obstacle_y = SCREEN_HEIGHT - OBSTACLE_HEIGHT - 10
obstacle_speed = 5

# 游戏状态
game_active = True
game_over = False


def draw_player():
    pygame.draw.rect(screen, GRAY, (player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT))


def draw_obstacle():
    pygame.draw.rect(screen, BLACK, (obstacle_x, obstacle_y, OBSTACLE_WIDTH, OBSTACLE_HEIGHT))


def check_collision():
    return (player_x < obstacle_x + OBSTACLE_WIDTH and
            player_x + PLAYER_WIDTH > obstacle_x and
            player_y < obstacle_y + OBSTACLE_HEIGHT and
            player_y + PLAYER_HEIGHT > obstacle_y)


def reset_game():
    global player_x, player_y, player_vel_y, is_jumping, game_active, game_over
    player_x = 100
    player_y = SCREEN_HEIGHT - PLAYER_HEIGHT - 10
    player_vel_y = 0
    is_jumping = False
    game_active = True
    game_over = False


# 游戏主循环
running = True
while running:
    screen.fill(WHITE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 按空格跳跃
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not is_jumping:
                is_jumping = True
                player_vel_y = jump_strength

        # 重新开始游戏
        if event.type == pygame.KEYDOWN and event.key == pygame.K_r and game_over:
            reset_game()

    # 游戏逻辑
    if game_active and not game_over:
        # 角色移动
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player_x -= 5
        if keys[pygame.K_RIGHT]:
            player_x += 5

        # 重力效果
        player_vel_y += gravity
        player_y += player_vel_y

        # 碰撞检测（地面）
        if player_y >= SCREEN_HEIGHT - PLAYER_HEIGHT - 10:
            player_y = SCREEN_HEIGHT - PLAYER_HEIGHT - 10
            is_jumping = False
            player_vel_y = 0

        # 障碍物移动
        obstacle_x -= obstacle_speed

        # 障碍物生成
        if obstacle_x + OBSTACLE_WIDTH < 0:
            obstacle_x = SCREEN_WIDTH
            obstacle_y = SCREEN_HEIGHT - OBSTACLE_HEIGHT - 10

        # 碰撞检测
        if check_collision():
            game_over = True

        # 绘制元素
        draw_player()
        draw_obstacle()

    # 游戏结束界面
    elif game_over:
        font = pygame.font.SysFont(None, 48)
        text = font.render("游戏结束！按 R 重新开始", True, BLACK)
        screen.blit(text, (SCREEN_WIDTH // 2 - 200, SCREEN_HEIGHT // 2 - 24))

    # 更新屏幕
    pygame.display.update()
    clock.tick(FPS)

pygame.quit()
